package com.ssy.lingxi.product.controller.pc;

import com.ssy.lingxi.common.controller.BaseController;
import com.ssy.lingxi.common.model.dto.UserLoginCacheDTO;
import com.ssy.lingxi.common.model.vo.PageVO;
import com.ssy.lingxi.common.response.PageData;
import com.ssy.lingxi.common.response.Wrapper;
import com.ssy.lingxi.product.api.model.request.BrandRequest;
import com.ssy.lingxi.product.api.model.request.CheckRequest;
import com.ssy.lingxi.product.api.model.request.SimpleIdRequest;
import com.ssy.lingxi.product.api.model.request.SimpleStatusRequest;
import com.ssy.lingxi.product.api.model.response.BrandResponse;
import com.ssy.lingxi.product.entity.Brand;
import com.ssy.lingxi.product.entity.BrandCheckRecord;
import com.ssy.lingxi.product.service.IBrandService;
import org.modelmapper.TypeToken;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @menu: 品牌管理
 * 品牌管理类
 * @author jw.chen
 * @version 2.0.0
 * @date 2020/6/28
 */
@RestController
@RequestMapping("/product/brand")
public class BrandController extends BaseController {

    @Resource
    private IBrandService brandService;

    /**
     * 查询品牌信息--商品能力
     * @param id 品牌id
     * @return Wrapper<Brand>
     */
    @RequestMapping(value = "/getBrand", method = RequestMethod.GET)
    public Wrapper<BrandResponse> getBrand(@RequestParam Long id) {
        UserLoginCacheDTO sysUser = this.getSysUser();
        Brand brand = brandService.getBrand(sysUser, id);
        if(brand != null){
            return Wrapper.success(this.modelMapper.map(brand, BrandResponse.class));
        }else{
            return Wrapper.success(null);
        }
    }

    /**
     * 查询品牌列表--商品能力
     * @param pageVO 分页实体
     * @param name 品牌名称
     * @param status 品牌状态(0-代表查全部,1-待提交审核,2-待审核,3-审核不通过,4-审核通过)
     * @return Wrapper<PageData<BrandResponse>>
     */
    @RequestMapping(value = "/getBrandList", method = RequestMethod.GET)
    public Wrapper<PageData<BrandResponse>> getBrandList(PageVO pageVO, @RequestParam("name") String name, @RequestParam(value = "status", required = false) Integer status) {
        UserLoginCacheDTO sysUser = this.getSysUser();
        Page<Brand> result = brandService.getBrandList(sysUser, pageVO, name, status);
        List<BrandResponse> resultList = this.modelMapper.map(result.getContent(), new TypeToken<List<BrandResponse>>(){}.getType());
        return Wrapper.success(new PageData<>(result.getTotalElements(), resultList));
    }

    /**
     * 查询待审核品牌列表--商品能力
     * @param pageVO 分页实体
     * @param status 品牌状态(0-代表查全部,2-待审核,3-审核不通过,4-审核通过)
     * @return Wrapper<PageData<Brand>>
     */
    @RequestMapping(value = "/getUnCheckBrandList", method = RequestMethod.GET)
    public Wrapper<PageData<BrandResponse>> getUnCheckBrandList(PageVO pageVO, @RequestParam(value = "name",defaultValue = "") String name, @RequestParam(value = "memberName",defaultValue = "") String  memberName, @RequestParam(value = "status") Integer status) {
        UserLoginCacheDTO sysUser = this.getSysUser();
        Page<Brand> result = brandService.getUnCheckBrandList(sysUser,pageVO, name,memberName,status);
        List<BrandResponse> resultList = this.modelMapper.map(result.getContent(), new TypeToken<List<BrandResponse>>(){}.getType());
        return Wrapper.success(new PageData<>(result.getTotalElements(), resultList));
    }

    /**
     * 审核品牌--商品能力
     * @param checkRequest 审核
     * @return 操作结果
     */
    @RequestMapping(value = "/checkBrand", method = RequestMethod.POST)
    public Wrapper<Long> checkBrand(@RequestBody CheckRequest checkRequest) {
        UserLoginCacheDTO sysUser = this.getSysUser();
        return Wrapper.success(brandService.checkBrand(sysUser, checkRequest));
    }

    /**
     * 新增/修改品牌--商品能力
     * @param brandRequest 品牌实体
     * @return 品牌id
     */
    @RequestMapping(value = "/saveOrUpdateBrand", method = RequestMethod.POST)
    public Wrapper<Long> saveOrUpdateBrand(@RequestBody BrandRequest brandRequest){
        UserLoginCacheDTO sysUser = this.getSysUser();
        Long siteId = this.getHeadersSiteId();
        brandRequest.setSiteId(siteId);
        return Wrapper.success(brandService.saveOrUpdateBrand(sysUser, brandRequest));
    }

    /**
     * 删除品牌--商品能力
     * @param simpleIdRequest 请求实体
     * @return Wrapper<String>
     */
    @RequestMapping(value = "deleteBrand", method = RequestMethod.POST)
    public Wrapper<String> deleteBrand(@RequestBody SimpleIdRequest simpleIdRequest){
        UserLoginCacheDTO sysUser = this.getSysUser();
        return Wrapper.success(brandService.deleteBrand(sysUser, simpleIdRequest.getId()));
    }

    /**
     * 启用/停用品牌--商品能力
     * @param simpleStatusRequest 请求实体
     * @return Wrapper<String>
     */
    @RequestMapping(value = "/updateBrandEnable", method = RequestMethod.POST)
    public Wrapper<String> updateBrandEnable(@RequestBody SimpleStatusRequest simpleStatusRequest) {
        UserLoginCacheDTO sysUser = this.getSysUser();
        return Wrapper.success(brandService.updateBrandEnable(sysUser, simpleStatusRequest.getId(), simpleStatusRequest.getIsEnable()));
    }

    /**
     * 提交审核品牌--商品能力
     * @param simpleIdRequest 品牌
     * @return Wrapper<String>
     */
    @RequestMapping(value = "/applyCheckBrand", method = RequestMethod.POST)
    public Wrapper<String> applyCheckBrand(@RequestBody SimpleIdRequest simpleIdRequest) {
        UserLoginCacheDTO sysUser = this.getSysUser();
        return Wrapper.success(brandService.applyCheckBrand(sysUser, simpleIdRequest.getId()));
    }

    /**
     * 查询品牌审核记录
     * @param brandId 品牌id
     * @return Wrapper<List<BrandCheckRecord>>
     */
    @RequestMapping(value = "/getBrandCheckRecord", method = RequestMethod.GET)
    public Wrapper<List<BrandCheckRecord>> getBrandCheckRecord(@RequestParam("brandId") Long brandId) {
        List<BrandCheckRecord> list = brandService.getBrandCheckRecord(brandId);
        return Wrapper.success(list);
    }

    /******************************************* 平台后台 *****************************************************/

    /**
     * 查询品牌列表--平台后台
     * @param pageVO 分页实体
     * @param name 品牌名称
     * @param status 品牌状态(0-代表查全部,2-待审核,3-审核不通过,4-审核通过)
     * @return Wrapper<PageData<Brand>>
     */
    @RequestMapping(value = "/getPlatformBrandList", method = RequestMethod.GET)
    public Wrapper<PageData<BrandResponse>> getPlatformBrandList(PageVO pageVO, @RequestParam("name") String name, @RequestParam(value = "status", required = false) Integer status) {
        Page<Brand> result = brandService.getPlatformBrandList(pageVO, name, status);
        List<BrandResponse> resultList = this.modelMapper.map(result.getContent(), new TypeToken<List<BrandResponse>>(){}.getType());
        return Wrapper.success(new PageData<>(result.getTotalElements(), resultList));
    }

    /**
     * 查询待审核品牌列表--平台后台
     * @param pageVO 分页实体
     * @param status 品牌状态(0-代表查全部,2-待审核,3-审核不通过,4-审核通过)
     * @return Wrapper<PageData<Brand>>
     */
    @RequestMapping(value = "/getPlatformUnCheckBrandList", method = RequestMethod.GET)
    public Wrapper<PageData<BrandResponse>> getPlatformUnCheckBrandList(PageVO pageVO,  @RequestParam(value = "status") Integer status) {
        Page<Brand> result = brandService.getPlatformUnCheckBrandList(pageVO, status);
        List<BrandResponse> resultList = this.modelMapper.map(result.getContent(), new TypeToken<List<BrandResponse>>(){}.getType());
        return Wrapper.success(new PageData<>(result.getTotalElements(), resultList));
    }

    /**
     * 查询品牌信息--平台后台
     * @param id 品牌id
     * @return Wrapper<Brand>
     */
    @RequestMapping(value = "/getPlatformBrand", method = RequestMethod.GET)
    public Wrapper<BrandResponse> getPlatformBrand(@RequestParam("id") Long id) {
        Brand brand = brandService.getBrand(id);
        if(brand != null){
            return Wrapper.success(this.modelMapper.map(brand, BrandResponse.class));
        }else{
            return Wrapper.success(null);
        }
    }

    /**
     * 审核品牌--平台后台
     * @param checkRequest 审核
     * @return Wrapper<Long>
     */
    @RequestMapping(value = "/checkPlatformBrand", method = RequestMethod.POST)
    public Wrapper<Long> checkPlatformBrand(@RequestBody CheckRequest checkRequest) {
        UserLoginCacheDTO sysUser = this.getSysUser();
        return Wrapper.success(brandService.checkPlatformBrand(sysUser, checkRequest));
    }

    /**
     * 查询品牌审核记录-平台后台
     * @param brandId 品牌id
     * @return Wrapper<List<BrandCheckRecord>>
     */
    @RequestMapping(value = "/getPlatformBrandCheckRecord", method = RequestMethod.GET)
    public Wrapper<List<BrandCheckRecord>> getPlatformBrandCheckRecord(@RequestParam("brandId") Long brandId) {
        List<BrandCheckRecord> list = brandService.getBrandCheckRecord(brandId);
        return Wrapper.success(list);
    }

    /**
     * 根据会员品类获取品牌
     *
     * @return
     */
    @GetMapping("/getBrandListByCustomerCategoryId")
    public Wrapper<List<BrandResponse>> getBrandListByCustomerCategoryId(@RequestParam("customerCategoryId") Long customerCategoryId) {
        List<BrandResponse> brandResponseList = brandService.getBrandListByCustomerCategoryId(customerCategoryId);
        return Wrapper.success(brandResponseList);
    }

    /**
     * 根据平台品类获取品牌
     *
     * @return
     */
    @GetMapping("/getBrandListByCategoryId")
    public Wrapper<List<BrandResponse>> getBrandListByCategoryId(@RequestParam("categoryId") Long categoryId) {
        List<BrandResponse> brandResponseList = brandService.getBrandListByCategoryId(categoryId);
        return Wrapper.success(brandResponseList);
    }


}
